package com.atguigu.dga.governance.assessor.calc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;


@Component("NO_ACCESS")
public class NoAccessAssessor extends Assessor {
    @Override
    protected void assessProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws ParseException {
        // 考评当日 和 最后访问日期做比较    差值天数 > days 则给0分
        String assessDate = assessParam.getAssessDate();
        Date tableLastAccessTime = assessParam.getTableMetaInfo().getTableLastAccessTime();

        Date assessDateDt = DateUtils.parseDate(assessDate, "yyyy-MM-dd");


        Date lastAccessDt = DateUtils.truncate(tableLastAccessTime, Calendar.DATE);
        long diffMs = assessDateDt.getTime() - lastAccessDt.getTime();

        //毫秒数换算为天数
        long diffDays = TimeUnit.DAYS.convert(diffMs, TimeUnit.MILLISECONDS);

        //提取参考值
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject metricJsonObj = JSON.parseObject(metricParamsJson);
        Integer days = metricJsonObj.getInteger("days");

        if(diffDays>days){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("距离最近一次访问天数："+diffDays+",超过"+days+"未被访问");
        }


    }
}
